package com.doit.demo02;

import java.io.*;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

@SuppressWarnings("all")

public class test03_chushibiao {
    public static void main(String[] args) throws IOException {

        method3();//复制的时候是追加的方式
//        method2();//复制到自己的文件中
//        method1();////复制到新等我文件中

    }
    public static void method1() throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("day21\\buffer\\1.txt"));
        BufferedWriter bw = new BufferedWriter(new FileWriter("day21\\buffer\\xin.txt"));

        Map<Integer,String> map = new TreeMap<>();

        String line;
        while ((line=br.readLine())!=null){
            String[] split = line.split("\\.");
            int key = Integer.parseInt(split[0]);
            String value = line; //如果里面也有"."的话.那怎么办?
            //所以采用值里面放全部信息,键只是用来排序
            map.put(key,line);
        }
        Set<Integer> set = map.keySet();
        for (Integer key : set) {
            bw.write(map.get(key)); //这边呢也是只写入值
            bw.newLine();
            bw.flush();
        }
        bw.close();
        br.close();
    }
    public static void method2() throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("day21\\buffer\\1.txt"));
        Map<String,String> map = new TreeMap<>();
        String line;
        while ((line=br.readLine())!=null){
            String[] split = line.split("\\.");
            map.put(split[0],split[1]);
        }
        br.close();

        //一样的名字就得这么写,不然数据会被覆盖
        BufferedWriter bw = new BufferedWriter(new FileWriter("day21\\buffer\\1.txt"));
        Set<String> strings = map.keySet();
        for (String string : strings) {
            bw.write(string+"."+map.get(string));
            bw.newLine();
            bw.flush();
        }
        bw.close();
    }
    public static void method3() throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("day21\\buffer\\1.txt"));
        Map<Integer,String>map=new TreeMap<>();
        String line;
        while ((line=br.readLine())!=null){
            String[] split = line.split("\\.");
            int key = Integer.parseInt(split[0]);
            String vaule = line;
            map.put(key,vaule );
        }
        br.close();

        BufferedWriter bw = new BufferedWriter(new FileWriter("day21\\buffer\\1.txt",true));
        bw.newLine();
        bw.newLine();
        Set<Integer> set = map.keySet();
        for (Integer key : set) {
            bw.write(map.get(key));
            bw.newLine();
            bw.flush();
        }
        bw.close();
    }
}
